package com.alibaba.citrus.ots.platform.enhance.functions.diff.sap.dataParser;

import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.sap.constant.ConsignmentOrderDiffStandardFieldDict;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.dataParser.DataParser;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.common.FieldLineDTO;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf017SDO;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: qiaojiange
 * @create: 2022/01/23
 * @Description sap主单 数据解析器
 */
@Component
public class SapIf017SDODataParse extends DataParser {
    /**
     * 报文中的字段，映射成对账处理的业务字段(标准字段)
     * key：报文中的字段，value：转换后的字段,这里的 value 字段也不要重复
     */
    private static Map<String, String> dataBaseFiledMapping = new HashedMap();

    static {
        //sap交货单号
        dataBaseFiledMapping.put("deliveryOrderId", ConsignmentOrderDiffStandardFieldDict.SAP_DELIVERY_ORDER_ID.getFieldCode());
        //sap销售单号(也是外部销售单号)
        dataBaseFiledMapping.put("salesOrderId", ConsignmentOrderDiffStandardFieldDict.SAP_SALES_ORDER_ID.getFieldCode());
        //平台销售单号
        dataBaseFiledMapping.put("outerSalesOrderId",ConsignmentOrderDiffStandardFieldDict.OTS_SALES_ORDER_ID.getFieldCode());
        //sap调拨单号 需要对上面两个字段进行翻转
        dataBaseFiledMapping.put("stoOrderId",  ConsignmentOrderDiffStandardFieldDict.SAP_STO_ORDER_ID.getFieldCode() );
        //销售组织
        dataBaseFiledMapping.put("salesOrgId", ConsignmentOrderDiffStandardFieldDict.SALES_ORG_ID.getFieldCode());
        //平台调拨单号
        dataBaseFiledMapping.put("outerStoOrderId", ConsignmentOrderDiffStandardFieldDict.OTS_STO_ORDER_ID.getFieldCode());
        //交货日期
        dataBaseFiledMapping.put("deliveryDate", ConsignmentOrderDiffStandardFieldDict.DELIVERY_DATE.getFieldCode());
    }


    @Override
    public Map<String, FieldLineDTO> parse(Object obj) {
        Map<String, FieldLineDTO> map = new HashMap<>();
        if (obj instanceof MnSapIf017SDO) {
            MnSapIf017SDO mnSapIf017SDO = (MnSapIf017SDO) obj;

            FieldLineDTO fieldLineDTO = FieldLineDTO.of("deliveryOrderId",
                    ConsignmentOrderDiffStandardFieldDict.SAP_DELIVERY_ORDER_ID.getFieldCode(),
                    SapAndCrmFieldDealUtils.generateIdField(mnSapIf017SDO.getDeliveryOrderId()));
            map.put(ConsignmentOrderDiffStandardFieldDict.SAP_DELIVERY_ORDER_ID.getFieldCode(), fieldLineDTO);


            fieldLineDTO = FieldLineDTO.of("salesOrderId",
                    ConsignmentOrderDiffStandardFieldDict.SAP_SALES_ORDER_ID.getFieldCode(),
                    SapAndCrmFieldDealUtils.generateIdField(mnSapIf017SDO.getSalesOrderId()));
            map.put(ConsignmentOrderDiffStandardFieldDict.SAP_SALES_ORDER_ID.getFieldCode(), fieldLineDTO);

            fieldLineDTO = FieldLineDTO.of("outerSalesOrderId",
                    ConsignmentOrderDiffStandardFieldDict.OTS_SALES_ORDER_ID.getFieldCode(),
                    SapAndCrmFieldDealUtils.generateIdField(mnSapIf017SDO.getOuterSalesOrderId()));
            map.put(ConsignmentOrderDiffStandardFieldDict.OTS_SALES_ORDER_ID.getFieldCode(), fieldLineDTO);

            fieldLineDTO = FieldLineDTO.of("outerStoOrderId",
                    ConsignmentOrderDiffStandardFieldDict.OTS_STO_ORDER_ID.getFieldCode(),
                    SapAndCrmFieldDealUtils.generateIdField(mnSapIf017SDO.getOuterStoOrderId()));
            map.put(ConsignmentOrderDiffStandardFieldDict.OTS_STO_ORDER_ID.getFieldCode(), fieldLineDTO);

            fieldLineDTO = FieldLineDTO.of("salesOrgId",
                    ConsignmentOrderDiffStandardFieldDict.SALES_ORG_ID.getFieldCode(),
                    SapAndCrmFieldDealUtils.generateIdField(mnSapIf017SDO.getSalesOrgId()));
            map.put(ConsignmentOrderDiffStandardFieldDict.SALES_ORG_ID.getFieldCode(), fieldLineDTO);

            fieldLineDTO = FieldLineDTO.of("stoOrderId",
                    ConsignmentOrderDiffStandardFieldDict.SAP_STO_ORDER_ID.getFieldCode(),
                    SapAndCrmFieldDealUtils.generateIdField(mnSapIf017SDO.getStoOrderId()));
            map.put(ConsignmentOrderDiffStandardFieldDict.SAP_STO_ORDER_ID.getFieldCode(), fieldLineDTO);

            Date date = SapAndCrmFieldDealUtils.toDate(mnSapIf017SDO.getDeliveryDate());
            fieldLineDTO = FieldLineDTO.of("deliveryDate",
                    ConsignmentOrderDiffStandardFieldDict.DELIVERY_DATE.getFieldCode(),
                    Objects.isNull(date)? null:date.toString());
            map.put(ConsignmentOrderDiffStandardFieldDict.DELIVERY_DATE.getFieldCode(), fieldLineDTO);
        }
        return map;
    }

    @Override
    public String obtainProcessType() {
        return "MnSapIf017SDO";
    }
}
